//
//  SetupPasswordViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "SetupPasswordViewController.h"

@interface SetupPasswordViewController ()

@end

@implementation SetupPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = self.forgetPassword ? @"找回登录密码" : @"请输入登录密码";
    self.passwordL.text = self.forgetPassword ? @"新密码" : @"确认密码";
    [self.registB setTitle:self.forgetPassword ? @"确定" : @"注册" forState:0];
    [self setupCustomBackItem];
}

- (IBAction)button_regist:(id)sender {
    
    [self.view endEditing:YES];
    
    NSString *loginPwd = [self.passwordTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *againLoginPwd = [self.confirmTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (loginPwd.length < 6) {
        [self.view makeToast:@"密码不能小于6位" duration:2.0f position:@"center"];
        return;
    }
    if (![loginPwd isEqualToString:againLoginPwd]) {
        [self.view makeToast:@"两次输入密码不一致" duration:2.0f position:@"center"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    if (self.forgetPassword) {
        [TDHttpEngine requestForUpdatePwdWithPwdType:@"1" updateType:@"2" value:self.validateCode newPwd:[self.passwordTF.text md5] custMobile:self.account complete:^(BOOL succeed, NSString *msg, NSString *cod) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            if (succeed) {
                [self.view makeToast:msg duration:2.0f position:@"center"];
                [self performSelector:@selector(popToLogin) withObject:self afterDelay:2.0f];
                
            }
            else{
                [self.view makeToast:msg duration:2.0f position:@"center"];
            }
        }];
    } else {
        [TDHttpEngine requestForRegistWithCustMobile:self.account custPwd:[self.passwordTF.text md5] complete:^(BOOL succeed, NSString *msg, NSString *cod) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.view makeToast:msg duration:2.0f position:@"center"];
            if (succeed) {
                [self performSelector:@selector(popToLogin) withObject:self afterDelay:2.0f];
            }
            else{
                
            }
        }];
    }
}

- (void)popToLogin
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
